package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Exception.BizException;
import com.itheima.Exception.NullParamException;
import com.itheima.Exception.SelectException;
import com.itheima.constants.CommonConstant;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
//import org.springframework.util.StringUtils;
//import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    //根据条件分页查询所有班级信息
    @Override
    public PageBean getAllClazz(EmpQueryParam cqp) {
        PageBean pageBean = new PageBean();

        PageHelper.startPage(cqp.getPage(),cqp.getPageSize());
        Page<Clazz> p =  clazzMapper.selectClazzs(cqp);
        p.forEach(clazz -> {
            if (clazz.getBeginDate().isAfter(LocalDate.now())){
                clazz.setStatus("未开班");
            } if (clazz.getBeginDate().isBefore(LocalDate.now())&&clazz.getEndDate().isAfter(LocalDate.now())){
                clazz.setStatus("在读");
            }if (clazz.getEndDate().isBefore(LocalDate.now())){
                clazz.setStatus("已结课");
            }
        });
        pageBean.setTotal(p.getTotal());//符合条件的行的数据
        pageBean.setRows(p);//符合条件的行数
        return pageBean;
    }

    @Override
    public void addClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insertClazz(clazz);
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @Override
    public Clazz getClazz(Integer id) {
        return clazzMapper.selectClazz(id);
    }

    @Override
    public void modifyClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);
    }

    /**
     * 根据id删除班级信息
     * @param id
     */
    @Override
    public void deleteClazz(Integer id) {
        clazzMapper.delete(id);
    }


    /**
     * 新增学院时中查询班级接口
     * @return
     */
    @Override
    public List<Clazz> getClazzs() {
        return clazzMapper.selectClazzName();
    }

    @Override
    public Clazz getClazzByName(String name) {
        if (StringUtils.isBlank(name)){
            log.error("ClazzServiceImpl getClazzByName ：name不能为空");
            throw new NullParamException(CommonConstant.PARAM_ERROR);
        }
        Clazz clazz  = clazzMapper.selectClazzByName(name, 5);
        if (clazz == null){
            log.error("ClazzServiceImpl getClazzByName clazz is null");
            throw new SelectException("查询班级为空异常");
        }
        if (StringUtils.equals(clazz.getRoom(),"212")){
            clazz.setName("yyyy");
        }
        return clazz;
    }

    @Override
    public void batchInsert(List<Clazz> list) {
        if (CollectionUtils.isEmpty(list)){
            log.error("ClazzServiceImpl batchInsert 参数不能为空");
            throw new BizException("ClazzServiceImpl batchInsert list 错误，不能为空");
        }
        clazzMapper.batchInsert(list);
    }

    @Override
    public void batchDeleteByIds(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            log.error("ClazzServiceImpl batchSelectByIds 参数不能为空");
            throw new BizException("ClazzServiceImpl batchSelectByIds 未传入任何id");
        }
        clazzMapper.batchDelete(idList);
    }

    @Override
    public List<Clazz> batchSelectByIds(List<Integer> idList) {
        //待查询内容id
        if (CollectionUtils.isEmpty(idList)) {
            log.error("ClazzServiceImpl batchSelectByIds 参数不能为空");
            throw new BizException("ClazzServiceImpl batchSelectByIds 未传入任何id");
        }
        //查询结果
        List<Clazz> clazzList = clazzMapper.batchSelectByIds(idList);
        if (CollectionUtils.isEmpty(clazzList)){
            log.error("ClazzServiceImpl batchSelectByIds 查询结果为空");
            throw new BizException("ClazzServiceImpl batchSelectByIds 查询结果为空");
        }
        return clazzList;
    }
}
